package com.ruoyi.salesman.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.salesman.domain.Salesman;
import com.ruoyi.salesman.domain.SalesmanApply;
import com.ruoyi.salesman.mapper.SalesmanApplyMapper;
import com.ruoyi.salesman.mapper.SalesmanMapper;
import com.ruoyi.salesman.service.ISalesmanApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务员审核Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class SalesmanApplyServiceImpl implements ISalesmanApplyService {
    @Autowired
    private SalesmanApplyMapper salesmanApplyMapper;
    @Resource
    private SalesmanMapper salesmanMapper;

    /**
     * 查询业务员审核
     *
     * @param id 业务员审核主键
     * @return 业务员审核
     */
    @Override
    public SalesmanApply selectSalesmanApplyById(Long id) {
        return salesmanApplyMapper.selectSalesmanApplyById(id);
    }

    /**
     * 查询业务员审核列表
     *
     * @param salesmanApply 业务员审核
     * @return 业务员审核
     */
    @Override
    public List<SalesmanApply> selectSalesmanApplyList(SalesmanApply salesmanApply) {
        return salesmanApplyMapper.selectSalesmanApplyList(salesmanApply);
    }

    /**
     * 新增业务员审核
     *
     * @param salesmanApply 业务员审核
     * @return 结果
     */
    @Override
    public int insertSalesmanApply(SalesmanApply salesmanApply) {
        salesmanApply.setCreateTime(DateUtils.getNowDate());
        return salesmanApplyMapper.insertSalesmanApply(salesmanApply);
    }

    /**
     * 修改业务员审核
     *
     * @param salesmanApply 业务员审核
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateSalesmanApply(SalesmanApply salesmanApply) {
        if (salesmanApply.getAuditStatus() == 1) {
            //成功  更改用户身份  添加业务员表
            String partnerInvitationCode = salesmanApplyMapper.selectPartnerUserByTel(salesmanApply.getPartnerTel());
            Integer updated = salesmanApplyMapper.updateUserIdentity(salesmanApply.getUserId(), 1, partnerInvitationCode);
            if (updated == 0) {
                throw new RuntimeException("审核失败");
            }
            String invitetionCode = salesmanApplyMapper.selectUserInvitetionCode(salesmanApply.getUserId());
            Salesman salesman = new Salesman();
            salesman.setUserId(salesman.getUserId());
            salesman.setSalesmanName(salesmanApply.getName());
            salesman.setSalesmanTel(salesmanApply.getTel());
            salesman.setSalesmanInvitationCode(invitetionCode);
            salesman.setSalesmanInvitedCode(partnerInvitationCode);
            salesmanMapper.insertSalesman(salesman);
        }
        salesmanApply.setUpdateTime(DateUtils.getNowDate());
        return salesmanApplyMapper.updateSalesmanApply(salesmanApply);
    }

    /**
     * 批量删除业务员审核
     *
     * @param ids 需要删除的业务员审核主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanApplyByIds(Long[] ids) {
        return salesmanApplyMapper.deleteSalesmanApplyByIds(ids);
    }

    /**
     * 删除业务员审核信息
     *
     * @param id 业务员审核主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanApplyById(Long id) {
        return salesmanApplyMapper.deleteSalesmanApplyById(id);
    }
}
